package com.infisight.hudprojector.fragment;

import java.util.ArrayList;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.infisight.hudprojector.R;
import com.infisight.hudprojector.adapter.TelInfoListAdapter;
import com.infisight.hudprojector.data.TelInfoClass;

import android.annotation.TargetApi;
import android.app.Activity;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.app.Fragment;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

/**
	电话到来显示模块
 */
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class NewPhoneFragment extends Fragment {
	String TAG = "NewPhoneFragment";
	SharedPreferences sharedPreferences = null;
	TelInfoClass telInfo = new TelInfoClass("1112223333", "2015-013-01", "周132132","",null);
	Gson gson= new Gson();
	int index = 0;
	TelInfoListAdapter telAdapter = new TelInfoListAdapter();
	ListView mListView ;
	List<TelInfoClass> lstTEL = new ArrayList<TelInfoClass>();
	List<TelInfoClass> lstTEL2  = new ArrayList<TelInfoClass>();
	/**
	 * 新建实例
	 * @return
	 */
	public static NewPhoneFragment newInstance(String strNewTelInfo)
	{
		NewPhoneFragment fragment = new NewPhoneFragment();
		Bundle args = new Bundle();
		args.putString("NEWTELINFO", strNewTelInfo);
		fragment.setArguments(args);
		return fragment;
	}
	/**
	 * 构造函数
	 */
	public NewPhoneFragment()
	{}
	/**
	 * 创建
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		if(getArguments() != null)
		{
			try{
				String strNewTelInfo = getArguments().getString("NEWTELINFO");
				telInfo = gson.fromJson(strNewTelInfo, TelInfoClass.class);
			}
			catch(Exception e)
			{}
		}
		
	}
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		Log.d(TAG, "onActivityCreated");
		InitView();
	}
	/**
	 * 构造配置文件
	 */
	private void InitView()
	{
		sharedPreferences = getActivity().getSharedPreferences("TelCongigXml", getActivity().MODE_PRIVATE); 
		mListView = (ListView) getActivity().findViewById(R.id.tel_Infos);
		SaveInfosToSP(sharedPreferences);
	}
	/**
	 * sp信息保存和读取
	 * 
	 * @param sharedPreferences
	 */
	public void SaveInfosToSP(SharedPreferences sp) {
		String spInfo = sp.getString("telInfos", null);
		if(index == 1){//清空
			sharedPreferences.edit().clear().commit();
		}else if(index == 0){ //添加
			gson = new Gson();	
			lstTEL = new ArrayList<TelInfoClass>();
			lstTEL2 = new ArrayList<TelInfoClass>();
			if (spInfo != null) {
				lstTEL = gson.fromJson(spInfo,new TypeToken<List<TelInfoClass>>() {}.getType());
			}
			lstTEL.add(telInfo);
			String retCRC = gson.toJson(lstTEL);
			sp.edit().putString("telInfos", retCRC).commit();
			if(retCRC != null){
				lstTEL2 = gson.fromJson(retCRC,
						new TypeToken<List<TelInfoClass>>() {
						}.getType());
			}
			telAdapter = new TelInfoListAdapter(lstTEL2, getActivity());
			mListView.setAdapter(telAdapter);
		}
	}
	/**
	 * 加载配置文件
	 */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_new_phone, container, false);
    }
}
